"""给WMS接口添加针对寄售的额外逻辑"""

from odoo import models, api, fields
from odoo.addons.nd_wms.controllers.wms import WMSAPI
from odoo.addons.nd_wms.models.wms_base import WMSConnectMixin


class ExtendWmsApi(WMSAPI):

    def _confirm_out_picking(self, post_data, picking_id):
        """添加对寄售的处理"""
        res = super()._confirm_out_picking(post_data, picking_id)
        # picking_id = self._get_available_picking(post_data['picking_id'])
        if post_data['order_type'] == 'JS':
            sale_id = picking_id.sale_id
            sale_id.write({'is_consignment_finish': True, 'consignment_date': fields.Date.today()})
            out_picking_id = sale_id.picking_ids.filtered(lambda x: x.picking_type_id.code == 'outgoing')
            # 在收到针对寄售出库单（ODOO的内部调拨单）的确认回传时，不仅将这些回传信息填充到内部调拨单上，同时也将其填充到真正出库单上
            super()._confirm_out_picking(post_data, out_picking_id, need_location=False)
        return res


# class WMSConnectMixinExt(models.AbstractModel):
#     _inherit = 'nd.wms'
#
#     post_process_and_send_data = WMSConnectMixin.post_process_and_send_data
#
#     @post_process_and_send_data('out_stock')
#     def push_consignment_stocks_to_wms(self, pickings):
#         """向WMS系统推送寄售出库单"""
#         # 寄售单的出库单是内部调拨单，而出库单则不同步
#         self.__check_pick_type(pickings, 'internal')
#         datas = self.odoo_out_pickings_to_wms(pickings, order_type='JS')
#         return datas
